package cn.com.hhrcw.admin.wx.web;

import cn.com.hhrcw.admin.wx.params.AccessTokenParams;
import cn.com.hhrcw.entity.CmsContent;
import cn.com.hhrcw.service.ICmsContentService;
import cn.com.hhrcw.wx.util.AccessTokenUtil;
import cn.com.hhrcw.wx.util.WxConfig;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.codec.digest.DigestUtils;
import org.jeecg.common.api.vo.Result;
import org.jeecg.common.aspect.annotation.AutoLog;
import org.jeecg.common.system.base.controller.JeecgController;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.validation.Valid;

/**
* 微信
*
* @author lixingxing
* @since 2020-11-06
*/
@RestController
@Slf4j
@RequestMapping("/admin/wx")
@Api(tags = "微信")
@Validated
public class WXAccessTokenController extends JeecgController<CmsContent, ICmsContentService> {


    @Autowired
    private WxConfig wxConfig;

    @Autowired
    private AccessTokenUtil accessTokenUtil;
    /**
    * 设置accessToken
    *
    * @param params 请求参数
    * @return
    */
    @PostMapping(value = "/setAccessToken")
    @AutoLog("设置accessToken")
    @ApiOperation(value = "设置accessToken", notes = "设置accessToken")
    public Result<?> setAccessToken(@RequestBody @Valid AccessTokenParams params) {

        log.info(params.toString());


        String newSig=  DigestUtils.md5Hex( params.getTimestamp()
                + wxConfig.getKey() +params.getWxid());
        log.info("the newSig is :"+newSig);
        if(!newSig.equalsIgnoreCase(params.getSig())){
            log.info("签名错误");
            return   Result.error(-1,"签名错误");
        }

        accessTokenUtil.setAccessToken(params.getWxid(),params.getAccessToken());

        log.info("the params.getWxid() is :"+params.getWxid());
        log.info("-----XXXX-----getAccessToken:"+accessTokenUtil.getAccessToken(params.getWxid()));

        return   Result.ok()  ;
    }


}
